import request from "@/utils/axios";
import { QueryParams, ArticleData, ArticleResponse } from "./types"
import type { AxiosResponse } from "axios";
/**
 * 用于调用 API 接口的函数
 * @method ArticleApi 获取文章相关
 */

// 文章相关接口
export default {
  // 获取文章列表或单个文章详情
  getArticles(params?: QueryParams): Promise<AxiosResponse<ArticleResponse>> {
    return request({
      url: '/api/article',
      method: 'get',
      params
    })
  },

  // 获取单个文章
  getArticle(articleId: string | number): Promise<AxiosResponse<ArticleData>> {
    return request({
      url: `/api/article/${articleId}`,
      method: 'get'
    })
  },

  // 创建文章
  createArticle(data: ArticleData): Promise<AxiosResponse<ArticleResponse>> {
    return request({
      url: '/api/article',
      method: 'post',
      data
    })
  },

  // 批量创建文章
  createArticles(articleList: ArticleData[]): Promise<AxiosResponse<ArticleResponse>> {
    return request({
      url: '/api/article',
      method: 'post',
      data: {
        ArticleList: JSON.stringify(articleList)
      }
    })
  },

  // 更新文章
  updateArticle(articleId: string | number, data: ArticleData): Promise<AxiosResponse<ArticleResponse>> {
    return request({
      url: `/api/article/${articleId}`,
      method: 'put',
      data
    })
  },

  // 更新文章敏感信息（审核状态、推荐状态等）
  updateSensitiveInfo(articleId: string | number, data: Partial<ArticleData>): Promise<AxiosResponse<ArticleResponse>> {
    return request({
      url: `/api/article/update/${articleId}`,
      method: 'put',
      data: new URLSearchParams(data as any) // 转换为form-data格式
    })
  },

  // 删除文章
  deleteArticle(articleId: string | number): Promise<AxiosResponse<ArticleResponse>> {
    return request({
      url: `/api/article/${articleId}`,
      method: 'delete'
    })
  }
}
